/*
 * Copyright (c) 2018.
 * http://www.ulabcare.com
 */

package com.mt.roll.upms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.Resource;
import com.mt.roll.core.dal.entity.SysMenuResource;
import com.mt.roll.core.dal.service.ResourceServiceBiz;
import com.mt.roll.core.dal.service.ServiceServiceBiz;
import com.mt.roll.core.dal.service.SysMenuResourceServiceBiz;
import com.mt.roll.core.enums.SubsystemEnum;
import com.mt.roll.core.params.EnabledParam;
import com.mt.roll.core.params.IdListParam;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.IdListVo;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.upms.params.ResourceParam;
import com.mt.roll.upms.params.ResourceQueryParam;
import com.mt.roll.upms.service.ResourceService;
import com.mt.roll.upms.vo.ResourceVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 资源服务实现类
 *
 * @author jiff
 * @date 2018/11/23
 * @since 1.0
 */
@Service("upmsResourceService")
@Transactional
public class ResourceServiceImpl extends BaseService implements ResourceService {
    @Autowired
    private ResourceServiceBiz resourceServiceBiz;
    @Autowired
    private ServiceServiceBiz serviceServiceBiz;
    @Autowired
    private SysMenuResourceServiceBiz sysMenuResourceServiceBiz;

    /**
     * 获取资源
     *
     * @param idParam 主键参数
     * @return 资源
     */
    @Override
    public R<ResourceVo> get(IdParam idParam) {
        Resource resource = resourceServiceBiz.getById(idParam.getId());
        Assert.notNull("资源不存在", resource);
        ResourceVo resourceVo = ResourceVo.builder().build();
        BeanUtils.copyProperties(resource, resourceVo);
        return successful(resourceVo);
    }

    /**
     * 获取资源列表
     *
     * @param resourceQueryParam 资源查询参数
     * @return 资源列表
     */
    @Override
    public R<List<ResourceVo>> list(ResourceQueryParam resourceQueryParam) {
        QueryWrapper<Resource> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotNull(resourceQueryParam.getServiceId()), Resource.SERVICE_ID, resourceQueryParam.getServiceId())
                .eq(ObjectUtils.isNotNull(resourceQueryParam.getSubsystem()), Resource.SUBSYSTEM, resourceQueryParam.getSubsystem())
                .eq(ObjectUtils.isNotNull(resourceQueryParam.getCategory()), Resource.CATEGORY, resourceQueryParam.getCategory())
                .eq(ObjectUtils.isNotNull(resourceQueryParam.getEnabled()), Resource.ENABLED, resourceQueryParam.getEnabled())
                .eq(StringUtils.isNotBlank(resourceQueryParam.getMethod()), Resource.METHOD, resourceQueryParam.getMethod())
                .like(StringUtils.isNotBlank(resourceQueryParam.getName()), Resource.NAME, resourceQueryParam.getName())
                .like(StringUtils.isNotBlank(resourceQueryParam.getUrl()), Resource.URL, resourceQueryParam.getUrl());
        List<Resource> resources = resourceServiceBiz.list(wrapper);
        List<ResourceVo> resourceVos = com.mt.roll.core.utils.BeanUtils.copyList(resources, ResourceVo.class);
        return successful(resourceVos);
    }

    /**
     * 获取资源分页列表
     *
     * @param resourceQueryParam 资源查询参数
     * @return 资源分页列表
     */
    @Override
    public R<PageVo<ResourceVo>> page(ResourceQueryParam resourceQueryParam) {
        QueryWrapper<Resource> wrapper = new QueryWrapper<>();
        wrapper.eq(ObjectUtils.isNotNull(resourceQueryParam.getServiceId()), Resource.SERVICE_ID, resourceQueryParam.getServiceId())
                .eq(ObjectUtils.isNotNull(resourceQueryParam.getSubsystem()), Resource.SUBSYSTEM, resourceQueryParam.getSubsystem())
                .eq(ObjectUtils.isNotNull(resourceQueryParam.getCategory()), Resource.CATEGORY, resourceQueryParam.getCategory())
                .eq(ObjectUtils.isNotNull(resourceQueryParam.getEnabled()), Resource.ENABLED, resourceQueryParam.getEnabled())
                .eq(StringUtils.isNotBlank(resourceQueryParam.getMethod()), Resource.METHOD, resourceQueryParam.getMethod())
                .like(StringUtils.isNotBlank(resourceQueryParam.getName()), Resource.NAME, resourceQueryParam.getName())
                .like(StringUtils.isNotBlank(resourceQueryParam.getUrl()), Resource.URL, resourceQueryParam.getUrl());
        IPage<Resource> page = resourceServiceBiz.page(new Page<>(resourceQueryParam.getCurrent(), resourceQueryParam.getSize()), wrapper);
        return successful(new PageVo<>(page, ResourceVo.class));
    }

    /**
     * 新增资源
     *
     * @param resourceParam 资源参数
     * @return 主键
     */
    @Override
    public R<IdVo> add(ResourceParam resourceParam) {
        Assert.notNull("服务不存在", serviceServiceBiz.getById(resourceParam.getServiceId()));
        Resource resource = new Resource();
        BeanUtils.copyProperties(resourceParam, resource);
        setCommonField(resource);
        resourceServiceBiz.save(resource);
        return successful(IdVo.builder().id(resource.getId()).build());
    }

    /**
     * 更新资源
     *
     * @param resourceParam 资源参数
     * @return 主键
     */
    @Override
    public R<IdVo> update(ResourceParam resourceParam) {
        Resource resource = resourceServiceBiz.getById(resourceParam.getId());
        Assert.notNull("资源不存在", resource);
        Assert.notNull("服务不存在", serviceServiceBiz.getById(resourceParam.getServiceId()));
        Resource updateResource = new Resource();
        BeanUtils.copyProperties(resourceParam, updateResource);
        setUpdateCommonField(updateResource);
        resourceServiceBiz.updateById(updateResource);
        return successful(IdVo.builder().id(resource.getId()).build());
    }

    /**
     * 启用停用
     *
     * @param enabledParam 启停参数
     * @return 主键
     */
    @Override
    public R<IdVo> enabled(EnabledParam enabledParam) {
        Resource resource = resourceServiceBiz.getById(enabledParam.getId());
        Assert.notNull("资源不存在", resource);
        Resource updateResource = new Resource().setId(enabledParam.getId());
        updateResource.setEnabled(enabledParam.getEnabled());
        setUpdateCommonField(updateResource);
        resourceServiceBiz.updateById(updateResource);
        return successful(IdVo.builder().id(resource.getId()).build());
    }

    /**
     * 删除资源
     *
     * @param idParam 主键参数
     * @return 主键
     */
    @Override
    public R<IdVo> delete(IdParam idParam) {
        Resource resource = resourceServiceBiz.getById(idParam.getId());
        Assert.notNull("资源不存在", resource);
        if (resource.getSubsystem() == null || resource.getSubsystem().equals(SubsystemEnum.COMMON.getValue())) {
            Assert.eqZero(sysMenuResourceServiceBiz.count(new QueryWrapper<SysMenuResource>().eq(SysMenuResource.RESOURCE_ID, resource.getId())), "资源还关联菜单");
        } else if (resource.getSubsystem().equals(SubsystemEnum.PLATFORM.getValue())) {
            Assert.eqZero(sysMenuResourceServiceBiz.count(new QueryWrapper<SysMenuResource>().eq(SysMenuResource.RESOURCE_ID, resource.getId())), "资源还关联菜单");
        }
        resourceServiceBiz.removeById(idParam.getId());
        return successful(IdVo.builder().id(resource.getId()).build());
    }

    /**
     * 批量删除资源
     *
     * @param idListParam 主键列表参数
     * @return 主键列表
     */
    @Override
    public R<IdListVo> delete(IdListParam idListParam) {
        return null;
    }
}
